package com.hanbang.taitaigou.ui.activity.aboutme.DistributionOfHeroes;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 我的——配送大侠
 * Created by fanxin on 2016/3/17.
 */
public class AboutMeDistributionOfHeroesActivity extends BaseActivity {
    /**
     * 提交按钮
     **/
    private Button button;
    /**
     * 姓名
     */
    private EditText name;
    /**
     * 电话
     */
    private EditText tel;
    /**
     * 地址
     */
    private EditText address;
    /**
     * 留言
     */
    private EditText message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutme_promotion_mbassador);
        initView();
        setOnClickListener();
    }

    //初始化
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_distribution_of_heroes), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));

        button = (Button) findViewById(R.id.button);
        button.setText("立刻成为配送大侠");

        name = (EditText) findViewById(R.id.et_about_me_name);
        tel = (EditText) findViewById(R.id.et_about_me_tel);
        address = (EditText) findViewById(R.id.et_about_me_address);
        message = (EditText) findViewById(R.id.et_about_me_message);
    }

    //设置点击事件
    private void setOnClickListener() {
        button.setOnClickListener(onClickListener);
    }

    //点击事件
    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //提交按钮
                case R.id.button:
                    Submit();

                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 提交信息按钮
     */
    private void Submit() {
        if (name.getText().toString().equals("")) {
            Toast.makeText(this, "姓名不能为空请输入...", Toast.LENGTH_LONG).show();
            return;
        }
        if (isMobileNO(tel.getText().toString())) {
        } else {
            Toast.makeText(this, "电话不正确", Toast.LENGTH_LONG).show();
            return;
        }
        if (address.getText().toString().equals("")) {
            Toast.makeText(this, "地址不能为空请输入...", Toast.LENGTH_LONG).show();
            return;
        }
        if (message.getText().toString().equals("")) {
            Toast.makeText(this, "留言不能为空请输入...", Toast.LENGTH_LONG).show();
            return;
        }
        HttpRequestParams httpRequestParams = new HttpRequestParams("AddUserOther");
        httpRequestParams.addUserId(userData);
        httpRequestParams.addBodyParameter("group_id", "2");
        httpRequestParams.addBodyParameter("User_name", name.getText().toString());
        httpRequestParams.addBodyParameter("Mobile", tel.getText().toString());
        httpRequestParams.addBodyParameter("Address", address.getText().toString());
        httpRequestParams.addBodyParameter("content", message.getText().toString());

        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在提交...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);

            }
        });

        Toast.makeText(this, "提交完成.", Toast.LENGTH_LONG).show();

    }

    /**
     * 手机号码的匹配
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
}
